<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>默认参数值</title>
</head>
<body>
  <script>
    function makeKing(name = 'Henry', numerals = 'Ⅷ') {
      return `King ${name} ${numerals}`;
    }
    console.log(makeKing());  // King Henry Ⅷ
    console.log(makeKing('Louis'));  // King Louis Ⅷ
    console.log(makeKing(undefined, 'Ⅵ'));  // King Henry Ⅵ
  </script>

  <script>
    function makeKing2(name = 'Henry') {
      name = 'Louis';
      return `King ${arguments[0]}`;
    }

    console.log(makeKing2());  // 'King undefined'
    console.log(makeKing2('Louis'));  // 'King Louis'
  </script>

  <script>
    let romanNumerals = ['Ⅰ', 'Ⅱ', 'Ⅲ', 'Ⅳ', 'Ⅴ', 'Ⅵ']
    let ordinality = 0;

    function getNumerals() {
      return romanNumerals[ordinality++];
    }

    function makeKing3(name = 'Henry', numerals = getNumerals()) {
      return `King ${name} ${numerals}`;
    }

    console.log(makeKing3());  // King Henry Ⅰ
    console.log(makeKing3('Louis', 'ⅩⅥ'));  // King Louis ⅩⅥ
    console.log(makeKing3());  // King Henry Ⅱ
    console.log(makeKing3());  // King Henry Ⅲ
  </script>

  <script>
    // 参数也存在于自己的作用域中，它们不能引用函数体的作用域
    function makeKing4(name = 'Henry', numerals = defaultNumeral) {  // 报错
      var defaultNumeral = 'Ⅷ';
      return `King ${name} ${numerals}`;
    }

    makeKing4();
  </script>
</body>
</html>